package cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.controller;

import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.entity.EquipmentClass;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.pojo.request.EquipmentClassRequest;
import cn.stylefeng.guns.modular.basicConfig.equipment.equipmentClass.service.EquipmentClassService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备控制器
 *
 * @author PANL
 * @since 2023/12/01 14:12
 */
@RestController
@ApiResource(name = "设备")
public class EquipmentClassController {

    @Resource
    private EquipmentClassService equipmentClassService;

    /**
     * 添加设备
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @PostResource(name = "添加设备", path = "/equipmentClass/add")
    public ResponseData<EquipmentClass> add(@RequestBody @Validated(EquipmentClassRequest.add.class) EquipmentClassRequest equipmentClassRequest) {
        EquipmentClass one = equipmentClassService.getOne(new LambdaQueryWrapper<EquipmentClass>().eq(EquipmentClass::getEqpClassCode, equipmentClassRequest.getEqpClassCode()));
        if (one != null) {
            return new ErrorResponseData<EquipmentClass>("500", "设备类型编码重复");
        }
        equipmentClassService.add(equipmentClassRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 删除设备
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @PostResource(name = "删除设备", path = "/equipmentClass/delete")
    public ResponseData<?> delete(@RequestBody @Validated(EquipmentClassRequest.delete.class) EquipmentClassRequest equipmentClassRequest) {
        equipmentClassService.del(equipmentClassRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 批量删除设备
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @PostResource(name = "批量删除设备", path = "/equipmentClass/batchDelete")
    public ResponseData<?> batchDelete(@RequestBody @Validated(BaseRequest.batchDelete.class) EquipmentClassRequest equipmentClassRequest) {
        equipmentClassService.batchDelete(equipmentClassRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 编辑设备
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @PostResource(name = "编辑设备", path = "/equipmentClass/edit")
    public ResponseData<?> edit(@RequestBody @Validated(EquipmentClassRequest.edit.class) EquipmentClassRequest equipmentClassRequest) {
        equipmentClassService.edit(equipmentClassRequest);
        return new SuccessResponseData<>();
    }

    /**
     * 查看设备详情
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @GetResource(name = "查看设备详情", path = "/equipmentClass/detail")
    public ResponseData<EquipmentClass> detail(@Validated(EquipmentClassRequest.detail.class) EquipmentClassRequest equipmentClassRequest) {
        return new SuccessResponseData<>(equipmentClassService.detail(equipmentClassRequest));
    }

    /**
     * 获取设备列表
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @GetResource(name = "获取设备列表", path = "/equipmentClass/list")
    public ResponseData<List<EquipmentClass>> list(EquipmentClassRequest equipmentClassRequest) {
        return new SuccessResponseData<>(equipmentClassService.findList(equipmentClassRequest));
    }

    /**
     * 获取设备列表（带分页）
     *
     * @author PANL
     * @since 2023/12/01 14:12
     */
    @GetResource(name = "获取设备列表（带分页）", path = "/equipmentClass/page")
    public ResponseData<PageResult<EquipmentClass>> page(EquipmentClassRequest equipmentClassRequest) {
        return new SuccessResponseData<>(equipmentClassService.findPage(equipmentClassRequest));
    }

}
